// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.

import "babel-polyfill";
import Vue from "vue";
import api from "@/common/api";

import promise from "es6-promise";
promise.polyfill();

import App from "./App";
import router from "./router/lazy";
import Antd from "ant-design-vue";
import "ant-design-vue/dist/antd.less";

import Viser from "viser-vue";
import axios from "axios";
import message from "ant-design-vue/es/message";
import store from "./store";
import PouchDB from "pouchdb";
// import VeeValidate from 'vee-validate';
import moment from "moment";
import "moment/locale/zh-cn";
moment.locale("zh-cn");

import feefilter from "./filter/feefilter";
// 全局导入过滤器
Object.keys(feefilter).forEach(key => Vue.filter(key, feefilter[key]));

import vuePayPwd from "vue-pay-pwd"; //输入密码
import VueClipboard from "vue-clipboard2"; //复制到剪切板插件
VueClipboard.config.autoSetContainer = true;

Vue.use(Antd);
Vue.use(VueClipboard);
Vue.use(vuePayPwd);

// axios.defaults.baseURL = '/agent';
axios.defaults.baseURL = "";
Vue.prototype.$axios = axios;
Vue.prototype.$message = message;
Vue.prototype.$moment = moment;

Vue.config.productionTip = false;
const config = {
  errorBagName: "errorBags", // change if property conflicts.
  fieldsBagName: "fieldBags"
};
Vue.use(Viser);
// Vue.use(VeeValidate);

message.config({
  top: `150px`,
  duration: 5,
  maxCount: 1
});

/* eslint-disable no-new */
new Vue({
  el: "#app",
  router,
  store,
  components: {
    App
  },
  template: "<App/>",
  mounted() {
    // var db = new PouchDB('admindb')
    // db.get('currUser').then(doc => {
    //     this.$store.commit('account/setuser', doc.user)
    // })
    // axios.interceptors.response.use(response => {
    //     if (response.data.code === 'NEED_LOGIN') {
    //         this.$router.push('/login')
    //         this.$store.commit('account/setuser', null)
    //     } else if (response.data.code === 'AUTH_FAIL') {
    //         this.$router.push('/exception/403')
    //         return new Promise(() => {})
    //     } else {
    //         return response
    //     }
    // }, error => {
    //     console.log(error)
    //     return new Promise(() => {})
    // })
  }
});
router.beforeEach((to, from, next) => {
  // Modal.destroyAll();
  if (to.meta.requireAuth) {
    // 判断该路由是否需要登录权限
    var token = localStorage.getItem("token");
    if (token) { // 通过localStorage获取当前的token是否存在
      // 进行axios请求，判断该路由是否有权限
      if (to.meta.toolParams){
        api.checkPermission({ code: to.meta.toolParams }, res => {
          if (res.code === "OK") {
            next();
          } else {
            next({
              path: "/not/permission"
            });
          }
        });
      }else{
        next()
      }

    } else {
      next({
        path: "/home",
        query: {
          redirect: to.fullPath
        } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      });
    }
  } else {
    next();
  }
  //   let allowBack = true // 给个默认值true
  //   if (to.meta.allowBack !== undefined) {
  //     allowBack = to.meta.allowBack
  //   }
  //   if (!allowBack) {
  //     history.pushState(null, null, location.href)
  //   }
  //   store.dispatch('updateAppSetting', {
  //     allowBack: allowBack
  //   })
});
